package com.hcy.orderapi.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcy.order_domain.entity.OrderPayment;
import com.hcy.order_domain.entity.ServerOrder;
import com.hcy.order_domain.service.impl.OrderPaymentServiceImpl;
import com.hcy.order_domain.service.impl.ServerOrderServiceImpl;
import com.hcy.ordermodel.dto.request.CustomerOrderListRequestDTO;
import com.hcy.ordermodel.dto.request.ManagerOrderRequestDTO;
import com.hcy.ordermodel.dto.request.ManagerPayOrderRequestDTO;
import com.hcy.ordermodel.dto.request.SimplePayOrderResponseDTO;
import com.hcy.ordermodel.dto.response.SimpleOrderResponseDTO;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicutils.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/5/13
 **/
@RestController
@Slf4j
@RequestMapping("/manager")
public class ManagerOrderController {

    @Autowired
    ServerOrderServiceImpl serverOrderService;

    @Autowired
    OrderPaymentServiceImpl orderPaymentService;

    /**
     * @Author huchenying
     * @Description  查询服务单
     * @Date 2022/5/13 21:59
     **/
    @PostMapping("/getServerOrderList")
    public ResponseData< MyPage<SimpleOrderResponseDTO>> getServerOrderList(@Validated @RequestBody ManagerOrderRequestDTO requestDTO){
        ResponseData<MyPage<SimpleOrderResponseDTO>> responseData = new ResponseData<>();
        LambdaQueryWrapper<ServerOrder> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getOrderNumber()),ServerOrder::getOrderNumber,requestDTO.getOrderNumber());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getCarId()),ServerOrder::getCarId,requestDTO.getCarId());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getDriverId()),ServerOrder::getDriverId,requestDTO.getDriverId());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getDriverPhone()),ServerOrder::getDriverPhone,requestDTO.getDriverPhone());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getServiceType()),ServerOrder::getServiceType,requestDTO.getServiceType());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPassengerPhone()),ServerOrder::getPassengerPhone,requestDTO.getPassengerPhone());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getStatus()),ServerOrder::getStatus,requestDTO.getStatus());
        queryWrapper.between(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getStartTimeStart()) &&
                CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getStartTimeEnd()),
                ServerOrder::getStartTime,requestDTO.getStartTimeStart(),requestDTO.getStartTimeEnd());

        Page<ServerOrder> page = serverOrderService.page(new Page<>(requestDTO.getPageNO(), requestDTO.getPageSize()), queryWrapper);
        MyPage<SimpleOrderResponseDTO> res = new MyPage<>(page.getCurrent(),page.getSize(),page.getTotal());
        List<SimpleOrderResponseDTO> responseDTOList = new ArrayList<>();
        for (ServerOrder record : page.getRecords()) {
            SimpleOrderResponseDTO simpleOrderResponseDTO = new SimpleOrderResponseDTO();
            BeanUtils.copyProperties(record,simpleOrderResponseDTO);
            responseDTOList.add(simpleOrderResponseDTO);
        }
        res.setRecords(responseDTOList);
        return responseData.success(res);
    }

    /**
     * @Author huchenying
     * @Description 查询支付单
     * @Date 2022/5/13 21:59
     **/
    @PostMapping("/getPayOrderList")
    public ResponseData<MyPage<SimplePayOrderResponseDTO>> getPayOrderList(@Validated @RequestBody ManagerPayOrderRequestDTO requestDTO){
        ResponseData<MyPage<SimplePayOrderResponseDTO>> responseData = new ResponseData<>();
        LambdaQueryWrapper<OrderPayment> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getOrderNumber()),OrderPayment::getOrderNumber,requestDTO.getOrderNumber());
        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPayNumber()),OrderPayment::getPayNumber,requestDTO.getPayNumber());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getStatus()),OrderPayment::getStatus,requestDTO.getStatus());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getCustomerId()),OrderPayment::getCustomerId,requestDTO.getCustomerId());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPayType()),OrderPayment::getPayType,requestDTO.getPayType());
        queryWrapper.between(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPayTimeStart()) &&
                CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPayTimeEnd()),
                OrderPayment::getPayTime,requestDTO.getPayTimeStart(),requestDTO.getPayTimeEnd());

        Page<OrderPayment> page = orderPaymentService.page(new Page<>(requestDTO.getPageNO(), requestDTO.getPageSize()), queryWrapper);
        MyPage<SimplePayOrderResponseDTO> res = new MyPage<>(page.getCurrent(),page.getSize(),page.getTotal());
        List<SimplePayOrderResponseDTO> responseDTOList = new ArrayList<>();
        for (OrderPayment record : page.getRecords()) {
            SimplePayOrderResponseDTO simpleOrderResponseDTO = new SimplePayOrderResponseDTO();
            BeanUtils.copyProperties(record,simpleOrderResponseDTO);
            responseDTOList.add(simpleOrderResponseDTO);
        }
        res.setRecords(responseDTOList);
        return responseData.success(res);
    }
}
